<?php

namespace app\service;

use app\model\ClueCommunicateRecord;
use app\model\ClueMobile;
use app\model\CluePerson;
use app\model\CluePersonCertificate;
use app\model\SystemCertificate;

/**
 * 客户管理-人才管理
 */
class CustomerPerson
{

    public function spliceWhere($params = [])
    {
        $cluePerson = new CluePerson();
        $person_ids = $cluePerson->getIds();
        $systemCert = new SystemCertificate();
        $cluePersonCert = new CluePersonCertificate();
        //人才姓名
        if (isset($params['person_name']) && $params['person_name']) {
//            $list = $cluePerson
//                ->where('person_name', 'like', '%' . $params['person_name'] . '%')
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $cluePerson
                ->where('person_name', 'like', '%' . $params['person_name'] . '%')
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $person_ids = $this->handlePersonIds($person_ids, $ids);
        }
        //身份证号码
        if (isset($params['person_id_card']) && $params['person_id_card']) {
//            $list = $cluePerson
//                ->where('person_id_card', 'like', '%' . $params['person_id_card'] . '%')
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $cluePerson
                ->where('person_id_card', 'like', '%' . $params['person_id_card'] . '%')
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $person_ids = $this->handlePersonIds($person_ids, $ids);
        }
        //电话
        if (isset($params['mobile']) && $params['mobile']) {
            $modelModel = new ClueMobile();
//            $list = $modelModel
//                ->where('mobile', $params['mobile'])
//                ->field('clue_p_id as id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $modelModel
                ->where('mobile', $params['mobile'])
                ->column('clue_p_id as id');
            //两个数组的交集并赋值给person_ids
            $person_ids = $this->handlePersonIds($person_ids, $ids);
        }
        //证书类别
        if (isset($params['certificate_leibie']) && $params['certificate_leibie']) {
//            $list = $cluePerson
//                ->where('certificate_leibie', $params['certificate_leibie'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $cluePerson
                ->where('certificate_leibie', $params['certificate_leibie'])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $person_ids = $this->handlePersonIds($person_ids, $ids);
        }
        //证书类型
        if (isset($params['certificate_leixing']) && $params['certificate_leixing']) {
//            $list = $cluePerson
//                ->where('certificate_leixing', $params['certificate_leixing'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $cluePerson
                ->where('certificate_leixing', $params['certificate_leixing'])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $person_ids = $this->handlePersonIds($person_ids, $ids);
        }
        //证书等级
        if (isset($params['certificate_dengji']) && $params['certificate_dengji']) {
//            $list = $cluePerson
//                ->where('certificate_dengji', $params['certificate_dengji'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $cluePerson
                ->where('certificate_dengji', $params['certificate_dengji'])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $person_ids = $this->handlePersonIds($person_ids, $ids);
        }
        //证书专业
        if (isset($params['certificate_zhuanye']) && $params['certificate_zhuanye']) {
//            $list = $cluePerson
//                ->where('certificate_zhuanye', $params['certificate_zhuanye'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $cluePerson
                ->where('certificate_zhuanye', $params['certificate_zhuanye'])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $person_ids = $this->handlePersonIds($person_ids, $ids);
        }

        //证书有效期
        if ((isset($params['certificate_period_of_validity_start']) && $params['certificate_period_of_validity_start']) && (isset($params['certificate_period_of_validity_end']) && $params['certificate_period_of_validity_end'])) {
            $params['certificate_period_of_validity_start'] .= ' 00:00:00';
            $params['certificate_period_of_validity_end'] .= ' 23:59:59';
//            $list = $cluePerson
//                ->where('certificate_period_of_validity', 'between', [$params['certificate_period_of_validity_start'], $params['certificate_period_of_validity_end']])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $cluePerson
                ->where('certificate_period_of_validity', 'between', [$params['certificate_period_of_validity_start'], $params['certificate_period_of_validity_end']])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $person_ids = $this->handlePersonIds($person_ids, $ids);
        }
        //b证
//        if (isset($params['certificate_b']) && ($params['certificate_b'] || (int)$params['certificate_b'] == 0)) {
            if (isset($params['certificate_b']) && ($params['certificate_b'])) {
//            $list = $cluePerson
//                ->where('certificate_b', $params['certificate_b'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $cluePerson
                ->where('certificate_b', $params['certificate_b'])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $person_ids = $this->handlePersonIds($person_ids, $ids);
        }

        //b证有效期
        if ((isset($params['certificate_b_period_of_validity_start']) && $params['certificate_b_period_of_validity_start']) && (isset($params['certificate_b_period_of_validity_end']) && $params['certificate_b_period_of_validity_end'])) {
            $params['certificate_b_period_of_validity_start'] .= ' 00:00:00';
            $params['certificate_b_period_of_validity_end'] .= ' 23:59:59';
//            $list = $cluePerson
//                ->where('certificate_b_period_of_validity','between', [$params['certificate_b_period_of_validity_start'], $params['certificate_b_period_of_validity_end']])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $cluePerson
                ->where('certificate_b_period_of_validity','between', [$params['certificate_b_period_of_validity_start'], $params['certificate_b_period_of_validity_end']])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $person_ids = $this->handlePersonIds($person_ids, $ids);
        }
        //社保
//        if (isset($params['social_security']) && ($params['social_security'] || (int)$params['social_security'] == 0)) {
            if (isset($params['social_security']) && ($params['social_security'] )) {
//            $list = $cluePerson
//                ->where('social_security', $params['social_security'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $cluePerson
                ->where('social_security', $params['social_security'])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $person_ids = $this->handlePersonIds($person_ids, $ids);
        }

        //其它证书
        if (isset($params['other_certificate']) && $params['other_certificate']) {
//            $list = $systemCert
//                ->where('certificate_name', 'like', '%' . $params['other_certificate'] . '%')
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
//            $clue_person_cert_list = $cluePersonCert
//                ->where('certificate_id', 'in', $ids)
//                ->field('clue_p_id')
//                ->select()
//                ->toArray();
//            $ids = array_column($clue_person_cert_list, 'clue_p_id');
            $ids = $systemCert
                ->where('certificate_name', 'like', '%' . $params['other_certificate'] . '%')
                ->column('id');
            $clue_person_cert_list = $cluePersonCert
                ->where('certificate_id', 'in', $ids)
                ->column('clue_p_id');
            $ids = array_column($clue_person_cert_list, 'clue_p_id');
            //两个数组的交集并赋值给person_ids
            $person_ids = $this->handlePersonIds($person_ids, $ids);
        }

        //创建时间
        if ((isset($params['create_start_date']) && $params['create_start_date']) && (isset($params['create_end_date']) && $params['create_end_date'])) {
            $params['create_start_date'] = $params['create_start_date'] . ' 00:00:00';
            $params['create_end_date'] = $params['create_end_date'] . ' 23:59:59';
            $list = $cluePerson->where('create_date', 'between', [$params['create_start_date'], $params['create_end_date']])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            $person_ids = $this->handlePersonIds($person_ids, $ids);
        }

        //分配时间
        if ((isset($params['allocation_start_date']) && $params['allocation_start_date']) && (isset($params['allocation_end_date']) && $params['allocation_end_date'])) {
            $params['allocation_start_date'] = $params['allocation_start_date'] . ' 00:00:00';
            $params['allocation_end_date'] = $params['allocation_end_date'] . ' 23:59:59';
            $list = $cluePerson->where('allocation_date', 'between', [$params['allocation_start_date'], $params['allocation_end_date']])
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            $person_ids = $this->handlePersonIds($person_ids, $ids);
        }

        //负责人 修改
        if (isset($params['f_uid']) && $params['f_uid']) {
            $userModel = new \app\model\User();
            //模糊查询用户id
            $userId = $userModel
                ->whereLike ('nickname','%'.$params['f_uid'].'%')
                ->column ('id');

            $list = $cluePerson
                ->where('f_uid', 'in', $userId)
                ->field('id')
                ->select()
                ->toArray();
            $ids = array_column($list, 'id');
            //两个数组的交集并赋值给person_ids
            $person_ids = $this->handlePersonIds($person_ids, $ids);
        }

        return $person_ids;
    }

    public function handlePersonIds($person_ids, $other_ids)
    {
        if(!$person_ids || !$other_ids){
            return [];
        }
        if ($person_ids && $other_ids) {
            $person_ids = array_intersect($person_ids, $other_ids);
        } elseif ($person_ids && !$other_ids) {
            $person_ids = [];
        } elseif (!$person_ids && $other_ids) {
            $person_ids = $other_ids;
        } elseif (!$person_ids && !$other_ids) {
            $person_ids = [];
        }
        return $person_ids;
    }
}